package yxy.game.pm2.command.worldboss;

import yxy.cherry.data.bean.menghuoboss;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.WorldBossMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.worldboss.WorldBossHandle;
import yxy.game.pm2.module.worldboss.WorldBossModule;
import yxy.game.pm2.module.worldboss.WorldBossPlayer;

/** 世界BOSS 世界BOSS挑战 */
@MessageType(proto = Command.WorldBossChanllenge, classtype = WorldBossMessage.WorldBossChanllengeRequest.class)
public class CMD_WorldBossChanllenge extends Command<WorldBossMessage.WorldBossChanllengeRequest> {

	@Override
	public void execute(MessageSource messageSource, WorldBossMessage.WorldBossChanllengeRequest request) {
		String playerCode = messageSource.getTag();

		WorldBossMessage.WorldBossChanllengeResponse.Builder response = WorldBossMessage.WorldBossChanllengeResponse.newBuilder();

		// 世界等级判断
		WorldBossHandle bossHandle = new WorldBossHandle();
		AreaBean areaBean = AreaModule.area(playerCode);
		Result result = bossHandle.checkWorldLevel(areaBean);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		WorldBossModule worldBossModule = WorldBossModule.get(playerCode);
		WorldBossPlayer worldBossPlayer = WorldBossPlayer.get(playerCode);

		try {
			if (worldBossModule.tryLock() && worldBossPlayer.tryLock()) {
				// 判断次数
				if (worldBossPlayer.remainTimes() <= 0) {
					if (worldBossPlayer.remainBuyTimes() <= 0) {
						send(messageSource, response, Result.ParamError);// 没有次数了 你的挑战次数用完了
						return;
					}
					// 有购买次数 直接花钱挑战
					menghuoboss data = bossHandle.data();
					int costNumber = data.getSpend();
					Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
					try {
						new CurrencyHandle().cost(playerCode, CCReason.七擒孟获, cost);
						worldBossPlayer.setBuytimes(worldBossPlayer.getBuytimes() + 1);
						worldBossPlayer.setTimes(worldBossPlayer.getTimes() - 1);
						worldBossPlayer.set();
					} catch (CurrencyNotEnoughException e) {
						send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
						return;
					}
				}

				result = bossHandle.battleResponseExecute(worldBossPlayer, worldBossModule, response);

				send(messageSource, response, result);
			}
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);
			e.printStackTrace();
		} finally {
			worldBossModule.unlock();
			worldBossPlayer.unlock();
		}

	}

	private void send(MessageSource messageSource, WorldBossMessage.WorldBossChanllengeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}